//
//  PaperModel.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/7/26.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperModel.h"

@implementation PaperModel

-(instancetype)init{
    if (self = [super init]) {
        _items = [NSMutableArray new];
    }
    return self;
}

-(instancetype)initWithDict: (NSDictionary*)dict{
    if (self = [super init]) {
        _items = [NSMutableArray new];
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"papersource"]) {
        _author = value[@"paperauthor"][@"text"];
        _score  = value[@"paperscore"][@"text"];
        _time   = value[@"papertime"][@"text"];
        _title  = value[@"papertitle"][@"text"];
        _uuid   = value[@"uuid"][@"text"];
        _comment = value[@"correct"][@"text"]; // 评论
    }
}

-(void)setValue:(id)value forKey:(NSString *)key{
    if ([key isEqualToString:@"questions"]) {
        id val = value[@"type"];
        if ([val isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dict in val) {
                if (dict.allKeys.count > 0) {
                    PaperItem *item = [[PaperItem alloc]initWithDict: dict];
                    [_items addObject:item];
                }
            }
        }
        else if([val isKindOfClass:[NSDictionary class]]){
            PaperItem *item = [[PaperItem alloc]initWithDict: val];
            [_items addObject:item];
        }
    }
    [super setValue:value forKey:key];
}

-(NSUInteger)totalQuestionsCount{
    NSUInteger i = 0;
    for (PaperItem *item in _items) {
        i += item.questions.count;
    }
    return i;
}

-(NSMutableArray *)collectedItems{
    NSMutableArray *arr = [NSMutableArray new];
    for (PaperItem *item in _items) {
        if (item.collectedQuestions.count > 0) {
            [arr addObject:item];
        }
    }
    return arr;
}

@end
